package com.nit.lab.controller;

import com.nit.lab.dto.DtoLoginUser;
import com.nit.lab.dto.DtoUser;
import com.nit.lab.dto.utils.DtoAjaxMsg;
import com.nit.lab.service.LoginService;
import com.nit.lab.utils.SystemConstants;
import org.jboss.logging.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.jvm.hotspot.debugger.cdbg.ProcessControl;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author CYT
 * @date 2017/11/19
 */
@Controller
@Scope("prototype")
@RequestMapping("/print_cyt")
public class LoginController {
    private static Logger log = Logger.getLogger(UserController.class.getClass());

    @Resource
    private LoginService loginService;

    @RequestMapping(value = "/findUserName")
    @ResponseBody
    public boolean findUserName(String userName) throws Exception {
        boolean result = loginService.findUserName(userName);
        return result;
    }

    @RequestMapping(value = "/findUserEmail")
    @ResponseBody
    public boolean findUserEmail(String userEmail) throws Exception {
        boolean result = loginService.findUserEmail(userEmail);
        return result;
    }

    @RequestMapping(value = "/findUserPhone")
    @ResponseBody
    public boolean findUserPhone(String userPhone) throws Exception {
        boolean result = loginService.findUserPhone(userPhone);
        return result;
    }

    @RequestMapping(value = "/selectUser")
    @ResponseBody
    public DtoAjaxMsg selectUser(String userName, String userCode) {
//        DtoAjaxMsg res = new DtoAjaxMsg();
        DtoAjaxMsg jsonDto = new DtoAjaxMsg();
        try {
            jsonDto = loginService.selectUser(userName, userCode);
            //将数据传入service层
//            res.put("result", result);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return jsonDto;
    }

    @RequestMapping(value = "/changCode")
    @ResponseBody
    public boolean changCode(String userEmail, String userPhone) throws Exception {
        boolean result = loginService.changCode(userEmail, userPhone);
        return result;
    }

    @RequestMapping(value = "/insertUserInfo")
    @ResponseBody
    public DtoAjaxMsg insertUserInfo(DtoLoginUser dtoLoginUser) throws IOException {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            Integer result = loginService.insertUserInfo(dtoLoginUser);
            if (result > SystemConstants.Insert_Success) {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_SUCCESS);
                //dtoAjaxMsg.ajaxInfo 为 string类型。
                //SystemConstants.INTERFACE_SUCCESS = "Success"
            } else {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
        }
        System.out.println("dtoAjaxMsg:" + dtoAjaxMsg.getAjaxInfo());
        return dtoAjaxMsg;
        //dtoAjaxMsg中有2个变量：String类的ajaxInfo 和 Object类的ajaxObject;
    }
}

